package com.atguigu.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/api/payment/alipay")

public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    @Autowired
    private PaymentService paymentService;


    @RequestMapping("submit/{orderId}")
    @ResponseBody
    public String alipay(@PathVariable Long orderId){
        String form = null;
        try {
            form = alipayService.createAliPay(orderId);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }

    // 设置一个回调callback/return
    @GetMapping("callback/return")
    public String returnCallback(){
        // 同步回调，返回一个给用户看的地址
        // "http://payment.gmall.com/pay/success.html" 支付成功的页面 跟web-all有关系
        // 重定向
        return "redirect:" + AlipayConfig.return_order_url;
    }

    // http://8uaa4q.natappfree.cc/api/payment/alipay/callback/notify
    @PostMapping("callback/notify")
    @ResponseBody
    public String callbackNotify(@RequestParam Map<String, String> paramMap){
        System.out.println("异步通知...");

        // 需要如何处理

        // Map<String, String> paramsMap = ... //将异步通知中收到的所有参数都存放到map中
        boolean signVerified = false; //调用SDK验证签名
        try {
            signVerified = AlipaySignature.rsaCheckV1(paramMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        // 获取交易状态
        String tradeStatus = paramMap.get("trade_status");

        // 验证out_trade_no 是否为商户系统中创建的订单号
        // 可以使用out_trade_no 查询交易记录中的数据
        String outTradeNo = paramMap.get("out_trade_no");
        PaymentInfo paymentInfoQuery = paymentService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
        if(paymentInfoQuery == null){
            return "failure";
        }

        // total_amount, seller_id , app_id
        // String totalAmount = paramMap.get("total_amount");
        // String sellerId = paramMap.get("seller_id");
        // String appId = paramMap.get("app_id");

        if(signVerified){
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商户自身业务处理，校验失败返回failure
            // 只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
            if("TRADE_SUCCESS".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)) {
                // 支付成功之后，需要更新交易记录状态
                // 更新payment_status , trade_no . callback_time ,callback_content
                paymentService.paySuccess(outTradeNo,PaymentType.ALIPAY.name(),paramMap);
                return "success";
            }
            return "failure";
        }else{
            // TODO 验签失败则记录异常日志，并在response中返回failure.
            return "failure";
        }

    }

    // 退款数据：
    // 我们需要传递订单id  order_info_id | order_info.out_trade_no
    // localhost:8205/api/payment/alipay/refund/220
    @GetMapping("refund/{orderId}")
    @ResponseBody
    public Result refund(@PathVariable Long orderId){
        // 调用服务层
        boolean flag = alipayService.refund(orderId);
        return Result.ok(flag);
    }

    // 定义一个关闭支付交易的url
    @GetMapping("closePay/{orderId}")
    @ResponseBody
    public boolean closePay(@PathVariable Long orderId){
        // 调用服务层方法
        Boolean flag = alipayService.closePay(orderId);
        // 直接返回
        return flag;
    }

    // 查看是否有交易记录
    @RequestMapping("checkPayment/{orderId}")
    @ResponseBody
    public Boolean checkPayment(@PathVariable Long orderId){
        // 调用服务层方法
        boolean flag = alipayService.checkPayment(orderId);
        return flag;
    }

    // 查询paymentInfo的控制器
    @GetMapping("getPaymentInfo/{outTradeNo}")
    @ResponseBody
    public PaymentInfo getPaymentInfo(@PathVariable String outTradeNo){
        PaymentInfo paymentInfo = paymentService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
        if (null!=paymentInfo){
            return paymentInfo;
        }
        return null;
    }


}
